package com.jason.kotlin.gank.ui.fragments

import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.imagebrowser.ImageBrowserUtils
import com.jason.kotlin.gank.model.GankDataBean
import com.jason.kotlin.gank.ui.adapter.GankGirlAdapter
import com.jason.kotlin.gank.ui.section.GankGirlSection
import com.jason.library.common.base.IViewSection
import com.jason.kotlin.gank.viewmodel.GankGirlViewModel
import com.jason.library.common.base.BaseStateFragment
import com.jason.library.common.ext.observe
import java.util.ArrayList

class GankGirlFragment : BaseStateFragment<GankGirlViewModel>() {

    private lateinit var refreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView

    private lateinit var girlAdapter: GankGirlAdapter

    private lateinit var section: IViewSection<List<GankDataBean>>

    companion object{
        fun newInstance(): GankGirlFragment {
            return GankGirlFragment()
        }
    }

    override fun bindLiveData() {

        observe(viewModel!!.randomData) {
            it.data?.let { it1 -> section.setData(it1) }
        }

        observe(viewModel!!.gankData) {
            refreshLayout.isRefreshing = false
            girlAdapter.loadMoreModule.isEnableLoadMore = true
            if (it.page == 1) {
                girlAdapter.setNewData(it.data as MutableList<GankDataBean>?)
            } else {
                if(it.data!!.isEmpty()) {
                    girlAdapter.loadMoreModule.isEnableLoadMore = false
                }else{
                    girlAdapter.addData(it.data!!)
                }

            }
            girlAdapter.loadMoreModule.loadMoreComplete()
        }
    }

    override fun initView(view: View) {
        refreshLayout = view.findViewById(R.id.swipeRefreshLayout)
        recyclerView = view.findViewById(R.id.recyclerView)
        section = GankGirlSection(refreshLayout.context)


        girlAdapter = GankGirlAdapter()
        girlAdapter.addHeaderView(section.rootView)

        girlAdapter.loadMoreModule.setOnLoadMoreListener {
            loadMoreData()
        }
        girlAdapter.loadMoreModule.isEnableLoadMore = false
        girlAdapter.loadMoreModule.isAutoLoadMore = true
        girlAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false

        val staggeredGridLayoutManager =
            StaggeredGridLayoutManager(
                2,
                StaggeredGridLayoutManager.VERTICAL
            )
        recyclerView.apply {
            layoutManager = staggeredGridLayoutManager
            adapter = girlAdapter
            itemAnimator = DefaultItemAnimator()
        }

        refreshLayout.setOnRefreshListener {
            loadData()
        }

        girlAdapter.setOnItemClickListener { adapter, view, position ->
            run {
                ImageBrowserUtils.open(activity, view, position,
                    viewModel!!.imageList as ArrayList<String>?,
                    adapter.data as ArrayList<GankDataBean>?
                )
            }
        }
    }

    private fun loadData() {
        girlAdapter.loadMoreModule.isEnableLoadMore = false
        viewModel!!.loadData()
    }


    private fun loadMoreData() {
        girlAdapter.loadMoreModule.isEnableLoadMore = false
        viewModel!!.loadMoreData()
    }


    override fun reload() {
        loadData()
    }

    override fun onDataInit() {
        loadData()
    }

    override fun getLayoutId() = R.layout.common_recycler_view

    override fun getPageTarget() = refreshLayout
}